package com.sy.controller;

import com.github.pagehelper.Page;
import com.sy.dto.HomeworkMagTable;
import com.sy.dto.HomeworkView;
import com.sy.dto.Table;
import com.sy.entity.Answer;
import com.sy.vo.HomeworkVo;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

@RestController
public class HomeworkController extends BaseController{
    @RequestMapping(value = "/homeworks",method = RequestMethod.POST)

    public void createHomework(@RequestBody HomeworkVo homeworkVo){
        //获取当前老师的id
        Integer teacherId = getSessionUserId();
        homeworkService.addHomework(homeworkVo, teacherId);
    }

    @RequestMapping(value = "/homeworks",method = RequestMethod.GET,params = {"page","limit"})
    public Table getHomeworkListByTeacherId(Integer page,Integer limit){
        //获取当前老师的id
        Integer teacherId = getSessionUserId();
        Page<HomeworkMagTable> homeworkList = homeworkService.getHomeworkListByTeacherId(teacherId, page, limit,"");
        return  getTable(homeworkList);
    }

    @RequestMapping(value = "/homeworks",method = RequestMethod.GET,params = {"page","limit","search"})
    public Table getHomeworkListByTeacherIdForPage(Integer page,Integer limit,String search) throws UnsupportedEncodingException {
        //获取当前老师的id
        Integer teacherId = getSessionUserId();
        String decode = URLDecoder.decode(search, "UTF-8");
        Page<HomeworkMagTable> homeworkList = homeworkService.getHomeworkListByTeacherId(teacherId, page, limit,decode);
        return  getTable(homeworkList);
    }

    @RequestMapping(value = "/homework/{id}",method = RequestMethod.DELETE)
    public boolean deleteHomework(@PathVariable("id") Integer homeworkId){
        return  homeworkService.deleteHomework(homeworkId);

    }
    @RequestMapping(value = "homework/{id}",method = RequestMethod.GET,params = {"type=view"})
    public HomeworkView viewHomework(@PathVariable("id")Integer homeworkId){

        return homeworkService.viewHomework(homeworkId);

    }
    @RequestMapping(value = "answer",method = RequestMethod.GET,params = {"homeworkId"})
    public List<Answer> getAnswer(Integer homeworkId){
        return homeworkService.getAnswer(homeworkId);
    }
//    @RequestMapping(value = "/homework/{id}",method = RequestMethod.GET)
//    public void editHomework(@PathVariable("id") Integer homeworkId){
//        System.out.println("-------------------------------------------------------------");
//        System.out.println(homeworkId);
//    }
}
